package com.ruoyi.life.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.life.service.IFeFineService;
import com.ruoyi.omp.api.domain.FeFine;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 违约金Controller
 *
 * @author cjie
 * @date 2021-09-23
 */
@Api(tags = " 违约金")
@RestController
@RequestMapping("/feFine")
public class FeFineController extends BaseController {
    @Autowired
    private IFeFineService feFineService;

    /**
     * 查询违约金列表
     */
    @ApiOperation(value="查询违约金列表", notes="查询违约金列表")
    @PreAuthorize(hasPermi = "life:feFine:list")
    @GetMapping("/list")
    public TableDataInfo list(FeFine feFine) {
        startPage();
        List<FeFine> list = feFineService.selectFeFineList(feFine);
        return getDataTable(list);
    }


    /**
     * 查询对应账单id的最后一条违约金记录
     * @param billId 账单id
     * @return
     */
    @GetMapping(value = "/feFine/listLastFine/{billId}")
    public R<FeFine> listLastFine(String billId){
        return R.ok(feFineService.listLastFine(billId));
    }


    /**
     * 导出违约金列表
     */
    @ApiOperation(value="导出违约金列表", notes="导出违约金列表")
    @PreAuthorize(hasPermi = "life:feFine:export")
    @Log(title = "违约金", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FeFine feFine) throws IOException {
        List<FeFine> list = feFineService.selectFeFineList(feFine);
        ExcelUtil<FeFine> util = new ExcelUtil<FeFine>(FeFine.class);
        util.exportExcel(response, list, "违约金数据");
    }
    /**
     * 获取违约金详细信息
     */
    @ApiOperation(value="获取违约金详细信息", notes="获取违约金详细信息")
    @PreAuthorize(hasPermi = "life:feFine:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(feFineService.selectFeFineById(id));
    }

    /**
     * 新增违约金
     */
    @ApiOperation(value="新增违约金", notes="新增违约金")
    @PreAuthorize(hasPermi = "life:feFine:add")
    @Log(title = "违约金", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FeFine feFine) {
        return toAjax(feFineService.insertFeFine(feFine));
    }

    /**
     * 新增违约金(内部调用)
     */
    @Log(title = "违约金", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R addFine(@RequestBody FeFine feFine) {
        return toR(feFineService.insertFeFine(feFine));
    }

    /**
     * 修改违约金(内部调用)
     */
    @Log(title = "违约金", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public R update(@RequestBody FeFine feFine) {
        return toR(feFineService.updateFeFine(feFine));
    }

    /**
     * 修改违约金
     */
    @ApiOperation(value="修改违约金", notes="修改违约金")
    @PreAuthorize(hasPermi = "life:feFine:edit")
    @Log(title = "违约金", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FeFine feFine) {
        return toAjax(feFineService.updateFeFine(feFine));
    }

    /**
     * 删除违约金
     */
    @ApiOperation(value="删除违约金", notes="删除违约金")
    @ApiImplicitParam(name = "ids", value = "违约金ID数组", required = true,paramType = "path", allowMultiple=true, dataType = "String")
    @PreAuthorize(hasPermi = "life:feFine:remove")
    @Log(title = "违约金", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(feFineService.deleteFeFineByIds(ids));
    }
}
